package com.lovo.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.lovo.model.Master;
import com.lovo.service.MasterService;
import com.lovo.service.impl.MasterServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "MasterServlet",urlPatterns = "/master")
public class MasterServlet extends HttpServlet {
    MasterService ms=new MasterServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html,charset=utf-8");
        String method=request.getParameter("method");
        if(method==null || method.equalsIgnoreCase("")){
            getMaster(request,response);
        }else if(method.equalsIgnoreCase("delete")){
           deleteMaster(request,response);
        }else if(method.equalsIgnoreCase("add")){
           addMaster(request,response);
        }else if(method.equalsIgnoreCase("update")){
            updateMaster(request,response);
        }
        else if(method.equalsIgnoreCase("show")){
            showMaster(request,response);
        }

    }
    protected void getMaster(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pn = request.getParameter("page");
        int pageNo = 1;
        if(pn != null){
            pageNo = Integer.parseInt(pn);
        }
        String ps = request.getParameter("maxResult");
        int pageSize = 1;
        if(ps != null){
            pageSize = Integer.parseInt(ps);
        }
        String value=request.getParameter("value");
        String masterNickname=null,masterName=null,masterTel=null;
        if(value!=null){
            if(value.equals("1")){
                masterNickname=request.getParameter("text");
            }else if(value.equals("2")){
                masterName=request.getParameter("text");
            } else if(value.equals("3")){
                masterTel=request.getParameter("text");
            }
        }

        Master master=new Master();
        master.setMasterNickname(masterNickname);
        master.setMasterName(masterName);
        master.setMasterTel(masterTel);
        System.out.println("master = " + master);
//        List<Master> list=ms.getAll(master);
        PageInfo<Master> pageInfo=ms.getPageAll(master,pageNo,pageSize);
        ObjectMapper objectMapper=new ObjectMapper();
        String json=objectMapper.writeValueAsString(pageInfo);
        System.out.println(json);
        response.getWriter().write(json);

    }
    protected void deleteMaster(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Integer id=Integer.parseInt(request.getParameter("masterId"));


        Master master=new Master();
        master.setMasterId(id);

        boolean flag=ms.delete(master);
        ObjectMapper objectMapper=new ObjectMapper();
        String json=objectMapper.writeValueAsString(flag);
        System.out.println(json);
        response.getWriter().write(json);

    }
    protected void addMaster(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String masterNickname=request.getParameter("masterNickname");
        String masterName=request.getParameter("masterName");
        String masterTel=request.getParameter("masterTel");
        String masterImages=request.getParameter("masterImager");
        String masterAddress=request.getParameter("masterAddress");
        String masterArea=request.getParameter("masterArea");

        Master master=new Master();
        master.setMasterNickname(masterNickname);
        master.setMasterName(masterName);
        master.setMasterTel(masterTel);
        master.setMasterAddress(masterAddress);
        master.setMasterImages(masterImages);
        master.setMasterArea(masterArea);
        System.out.println("masterTel = " + masterTel);
        System.out.println("masterTel = " + masterArea);
        System.out.println("masterTel = " + masterAddress);
        boolean flag=ms.insert(master);
        ObjectMapper objectMapper=new ObjectMapper();
        String json=objectMapper.writeValueAsString(flag);
        System.out.println(json);
        response.getWriter().write(json);
    }
    protected void updateMaster(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Master master=new Master();
        String id=request.getParameter("masterId");
        if(id!=null){
            master.setMasterId(Integer.parseInt(id));
        }
        String masterNickname=request.getParameter("masterNickname");
        String masterName=request.getParameter("masterName");
        String masterTel=request.getParameter("masterTel");
        String masterImages=request.getParameter("masterImages");
        String masterAddress=request.getParameter("masterAddress");
        String masterArea=request.getParameter("masterArea");

        master.setMasterNickname(masterNickname);
        master.setMasterName(masterName);
        master.setMasterTel(masterTel);
        master.setMasterAddress(masterAddress);
        master.setMasterImages(masterImages);
        master.setMasterArea(masterArea);
        System.out.println("masterArea====="+masterArea);
        System.out.println(id);
        boolean flag=ms.update(master);
        ObjectMapper objectMapper=new ObjectMapper();
        String json=objectMapper.writeValueAsString(flag);
        System.out.println(json);
        response.getWriter().write(json);
    }
    protected void showMaster(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Master master=new Master();
        String id=request.getParameter("masterId");
        if(id!=null){
            master.setMasterId(Integer.parseInt(id));
        }
        Master m=ms.show(master);
        ObjectMapper objectMapper=new ObjectMapper();
        String json=objectMapper.writeValueAsString(m);
        System.out.println(json);
        response.getWriter().write(json);

    }

}
